import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import store from "@/store";
import { ElMessage } from "element-plus";
import i18n from "@/locales";
import NProgress from "nprogress";
let _isMobile = () => {
  const flag = navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  );
  return flag;
};

let startTime = Date.now();
let currentTime;
// 配置进度条
NProgress.inc();
NProgress.configure({ showSpinner: false });
const routerHistory = createWebHashHistory();
const router = createRouter({
  history: routerHistory,
  routes: [
    {
      path: "/",
      name: "communityHome",
      component: () =>
        import("@/views/community/views/communityHome/communityHome.vue"),
    },
    // {
    //   path: "/",
    //   name: "home",
    //   component: () => import("../views/home/index.vue"),
    //   meta: {
    //     keepAlive: true, //设置页面是否需要使用缓存
    //     idx: 0,
    //     cacheImages: true,
    //   },
    // },
   
    {
      path: "/upload",
      name: "upload",
      component: () => import("@/views/home/components/upload.vue"),
      meta: {
        idx: 0,
      },
    },
    {
      path: "/document",
      name: "document",
      component: () => import("@/views/home/components/document.vue"),
      meta: {
        idx: 0,
      },
    },
    {
      path: "/gouserinfo",
      name: "gouserinfo",
      component: () => import("@/views/home/components/gouserinfo.vue"),
      meta: {
        idx: 0,
        cacheImages: true,
      },
    },
    {
      path: "/accoutSetting",
      name: "accoutSetting",
      component: () => import("@/views/home/components/accoutSetting.vue"),
      meta: {
        idx: 0,
      },
    },

    {
      path: "/detailInfo",
      name: "detailInfo",
      component: () => import("@/views/home/components/detailInfo.vue"),
      meta: {
        cacheImages: true,
      },
    },
    {
      path: "/collectlist",
      name: "collectlist",
      component: () => import("@/views/home/components/collectlist.vue"),
    },
    {
      path: "/searchNew",
      name: "searchNew",
      component: () => import("@/views/home/components/searchNew.vue"),
      meta: {
        cacheImages: true,
        key: "main",
      },
    },
    {
      path: "/uploadModel",
      name: "uploadModel",
      component: () => import("@/views/home/components/uploadModel/index.vue"),

      meta: {
        idx: 0,
      },
    },
   
  
    {
      path: "/hotcommunity",
      name: "hotcommunity",
      component: () => import("@/views/community/components/hotcommunity.vue"),
      meta: {
        cacheImages: true,
      },
    },
    {
      path: "/morecircle",
      name: "morecircle",
      component: () => import("@/views/community/components/morecircle.vue"),
      meta: {
        cacheImages: true,
      },
    },
    {
      path: "/pages/tags/tags",
      name: "hotdetai",
      component: () => import("@/views/community/components/hotdetai.vue"),
      meta: {
        cacheImages: true,
      },
    },
  
    {
      path: "/pages/user/user",
      name: "userdetailCommunity",
      component: () =>
        import("@/views/community/components/userdetailCommunity.vue"),
      meta: {
        cacheImages: true,
      },
    },
    {
      path: "/searchCommunity",
      name: "searchCommunity",
      component: () =>
        import("@/views/community/components/searchCommunity.vue"),
      meta: {
        cacheImages: true,
      },
    },
    {
      path: "/pages/circle/list",
      name: "joincommunity",
      component: () => import("@/views/community/components/joincommunity.vue"),
      meta: {
        cacheImages: true,
      },
    },
   
    {
      path: "/community",
      name: "community",
      redirect: "/communityHome",
      component: () => import("@/views/community/index.vue"),
    },

    // meta: {
    //   cacheImages: true,
    //   hideHeade: true,
    //   hidewidth: true,
    // },
    // children: [

    {
      path: "/uploadVideo",
      name: "uploadVideo",
      component: () =>
        import("@/views/community/views/releaseVideos/uploadVideo.vue"),
    },

    {
      path: "/pages/sticky/sticky",
      name: "communityDetail",
      component: () =>
        import("@/views/community/views/communityDetail/communityDetail.vue"),
      meta: {
        // hideHeade:true
      },
    },
    {
      path: "/pages/common/search",
      name: "communitySearch",
      component: () =>
        import("@/views/community/views/communitySearch/communitySearch.vue"),
    },
  ],
});

// router.beforeEach((to, from, next) => {
//   // if (_isMobile()) {
//   //     window.location.href = "http://sport.goaldice.com/football/Phone/#/";

//   // } else {
//   if (to.meta.hasLogin && !store.state.account) {
//     ElMessage.warning(i18n.global.t("index.placeLinkWallet"));
//     if (from.path != "/game") {
//       router.push("/");
//     }
//     return;
//   }
//   window.scrollTo(0, 0);
//   store.commit("setHideHeaderBg", false);

//   next();
//   // }
// });

router.beforeEach((to, from, next) => {
  store.commit("setShowBottom", true);

  // 开启进度条
  NProgress.start();
  // 设置图片缓存策略
  if (to.matched.some((record) => record.meta.cacheImages)) {
    // 缓存7天，根据需要进行调整
    const maxAge = 7 * 24 * 60 * 60;
    const headers = {
      "Cache-Control": `max-age=${maxAge}`,
    };
    to.meta.headers = headers;
  }

  let toPath = to.path;
  window.scrollTo(0, 0);
  // 如果进入的是首页则修改头部背景色透明 否则头部背景色白色
  console.log(to.path);
  if (to.path === "/") {
    store.commit("setHideHeaderBg", true);
  } else {
    store.commit("setHideHeaderBg", false);
  }

  // if (_isMobile()) {
  //     window.location.href = "http://sport.goaldice.com/football/Phone/#/";
  // } else {
  // if (to.meta.hasLogin && !store.state.account) {
  //     ElMessage.warning(i18n.global.t("index.placeLinkWallet"));
  //     if (from.path != "/game") {
  //         router.push("/");
  //     }
  //     return;
  // }
  if (to) {
    // 第一步：页面跳转后记录一下当前的时间 currentTime
    currentTime = Date.now();

    let datas = {};
    datas.name = from.name;
    datas.time = parseInt((currentTime - startTime) / 1000);
    if (from.name) {
      store.commit("saveUserDataInfo", {
        type: "dwellTime",
        data: datas,
      });
    }

    // 第三步：每次都要初始化一下 startTime
    startTime = Date.now();
  }
  next();
  // }
});
router.afterEach(() => {
  // 关闭进度条
  NProgress.done();
});
export default router;
